package com.wu.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/****
 * 解析用户分组信息
 * @author EDY
 *
 */
@Slf4j
public class AnalysisUserGroup {

	/*****
	 * 根据匹配规则解析用户分组
	 * @param abExperiment
	 * @param matchRule    eg:dailyReco:(a|b)
	 * @param aesKey
	 * @param aesIv
	 * @return
	 */
	public static String userGroupAnalysis(String abExperiment, String matchRule, String aesKey, String aesIv) {
		try {
			if (StringUtils.isNotBlank(abExperiment)) {
				abExperiment = AesUtils.aesDecryption(aesKey, aesIv, abExperiment);
				List<String> list = new ArrayList<>();
				list.addAll(Arrays.asList(abExperiment.split(",")));
				list.removeIf(test -> !test.matches(matchRule));
				if (list.size() == 1) {
					return list.get(0);
				}
			}
		}catch (Exception e) {
			log.warn("{用户分组解析失败,入参："+abExperiment+",匹配规则为："+matchRule+"}------"+e);
		}
		return null;
	}
}
